package com.terabits.smartriver.service.impl;

import com.terabits.smartriver.constants.DeviceEnum;
import com.terabits.smartriver.mapper.*;
import com.terabits.smartriver.meta.po.*;
import com.terabits.smartriver.meta.vo.MaintenanceVO;
import com.terabits.smartriver.service.DeviceService;
import com.terabits.smartriver.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class DeviceServiceImpl extends BaseService implements DeviceService {
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private DeviceMaintenanceMapper maintenanceMapper;
    @Resource
    private RegionMapper regionMapper;
    @Resource
    private MaintainMapper maintainMapper;

    @Override
    public List<PageData> getDevice(PageData pageData) {
        return deviceMapper.selectDevice(pageData);
    }

    @Override
    public List<Device> getDevices(PageData params) {
        Object boxNum = params.get("boxNum");
        Object boxInterface = params.get("boxInterface");
        Device device = new Device();
        if (!StringUtils.isEmpty(boxNum)) {
            device.setBoxNum(String.valueOf(boxNum));
        }
        if (!StringUtils.isEmpty(boxInterface)) {
            device.setBoxInterface(String.valueOf(boxInterface));
        }
        List<Device> select = deviceMapper.select(device);
        return select;
    }

    @Override
    public Device getOneDevice(String device_num) {
        Device device = new Device();
        device.setDeviceNum(device_num);
        List<Device> select = deviceMapper.select(device);
        if (select.size() != 0) {
            return select.get(0);
        }
        return null;
    }

    @Override
    public int addDevice(Device device) {
        List devices = findObjectsByProperty("deviceNum", device.getDeviceNum(), deviceMapper, Device.class);
        if (devices != null && devices.size() == 1) {
            return -1;
        }
        return deviceMapper.insertSelective(device);
    }

    @Override
    public int updateDevice(Device device) {
        int res = deviceMapper.updateByPrimaryKeySelective(device);
        return res;
    }

    @Override
    public int deleteDevice(Integer id) {
        String deviceNum = deviceMapper.selectByPrimaryKey(id).getDeviceNum();
        String idsSQL = "SELECT GROUP_CONCAT(id) ids FROM model_device WHERE devices ='" + deviceNum + "' OR devices LIKE '" + deviceNum + ",%' OR devices LIKE '%," + deviceNum + "' OR devices LIKE '%," + deviceNum + ",%' ";
        String ids = regionMapper.selectStrSQL(idsSQL);
        regionMapper.deleteSQL("DELETE FROM model_device WHERE id IN (" + ids + ")");
        return deviceMapper.deleteByPrimaryKey(id);
    }


    //======================保 养 模 块=========================//

    @Override
    public List<PageData> getDeviceMaintenance(PageData pageData) {
        return maintainMapper.getMaintain(pageData);
    }


}
